package com.yuda.tools.authreport.controller;

import com.yuda.common.core.utils.file.ZipUtils;
import com.yuda.common.core.web.controller.BaseController;
import com.yuda.common.core.web.domain.AjaxResult;
import com.yuda.tools.authreport.entity.StudentInfo;
import com.yuda.tools.authreport.service.StudentInfoService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class DownloadTranscriptController extends BaseController {

    @Autowired
    private StudentInfoService studentInfoService;


    @GetMapping("/check/{message}")
    public AjaxResult check(@PathVariable("message") String message) {

        startPage();//分页

        List<StudentInfo> list = studentInfoService.selectStu(message);
        if (list == null) {
            return AjaxResult.error("无该学生相关购买记录");
        } else
            return AjaxResult.success(list);
    }


    @GetMapping("/downloadAll/{message}")
    public synchronized void downloadAll(@PathVariable("message") String message, HttpServletResponse response) throws IOException, InterruptedException {

        List<StudentInfo> list = studentInfoService.selectStu(message);
//        System.out.println("++++++++++++++");
//        System.out.println(list.size());
//        System.out.println("++++++++++++++");
        if (list == null) {
            response.getWriter().print("无该学生相关购买记录");
        } else {
            List<File> fileList = new ArrayList<>();

            for (StudentInfo studentInfo : list) {

                String ExamUuid = studentInfo.getExamUuid();
                ExamUuid = ExamUuid.trim();//截取日期部分
                String date = "";
                if (ExamUuid != null && !"".equals(ExamUuid)) {
                    for (int i = 0; i < ExamUuid.length(); i++) {
                        if (ExamUuid.charAt(i) >= 48 && ExamUuid.charAt(i) <= 57) {
                            date += ExamUuid.charAt(i);
                        }
                    }
                    String idCard = studentInfo.getIdCard();
                    System.out.println(date);
                    String srcPath = "/mnt/auth_pdf/" + ExamUuid + "/" + idCard + ".pdf";
                    File srcFile = new File(srcPath);
                    String newName = "成绩单-" + date+".pdf";

                    File filepath = new File("/mnt/auth_pdf/gc");
                    if(!filepath.isDirectory()){
                        filepath.mkdirs();
                    }
                    File newFile = new File(filepath.getAbsolutePath()+"/"+newName);
//                    System.out.println("============  "+filepath.getAbsolutePath()+"/"+newName);
                    FileUtils.copyFile(srcFile,newFile);
                    fileList.add(newFile);
                }
            }
//            System.out.println("********************");
//            System.out.println(fileList.size());
//            System.out.println("********************");


            while (fileList.size() != list.size()) {
                Thread.sleep(1000L);
            }
            Thread.sleep(1000L);
            ZipUtils.toZip(fileList, response.getOutputStream());
            Thread.sleep(1000L);
            for (File file : fileList) {
                if(file.exists()) {
                    file.delete();
                }

            }
        }
    }



    @GetMapping("/downloadOne/{message}/{examUuid}")
    public synchronized void downloadOne(@PathVariable("message") String message,@PathVariable("examUuid")String examUuid ,HttpServletResponse response) throws IOException, InterruptedException {

        List<StudentInfo> list = studentInfoService.selectStu(message);
        List<StudentInfo> list01 = new ArrayList<>();
        for (StudentInfo studentInfo : list) {
            if(studentInfo.getExamUuid().equals(examUuid))
                list01.add(studentInfo);
        }

        System.out.println("++++++++++++");
        System.out.println(list01.size());
        System.out.println("++++++++++++");


        if (list01 == null) {
            response.getWriter().print("无该学生相关购买记录");
        } else {
            List<File> fileList = new ArrayList<>();

            for (StudentInfo studentInfo : list01) {

                String ExamUuid = studentInfo.getExamUuid();
                ExamUuid = ExamUuid.trim();//截取日期部分
                String date = "";
                if (ExamUuid != null && !"".equals(ExamUuid)) {
                    for (int i = 0; i < ExamUuid.length(); i++) {
                        if (ExamUuid.charAt(i) >= 48 && ExamUuid.charAt(i) <= 57) {
                            date += ExamUuid.charAt(i);
                        }
                    }
                    String idCard = studentInfo.getIdCard();
                    System.out.println(date);
                    String srcPath = "/mnt/auth_pdf/" + ExamUuid + "/" + idCard + ".pdf";
                    File srcFile = new File(srcPath);
                    String newName = "成绩单-" + date+".pdf";

                    File filepath = new File("/mnt/auth_pdf/gc");
                    if(!filepath.isDirectory()){
                        filepath.mkdirs();
                    }
                    File newFile = new File(filepath.getAbsolutePath()+"/"+newName);
                    System.out.println("============  "+filepath.getAbsolutePath()+"/"+newName);
                    FileUtils.copyFile(srcFile,newFile);
                    fileList.add(newFile);
                }
            }
            System.out.println("********************");
            System.out.println(fileList.size());
            System.out.println("********************");


            while (fileList.size() != list01.size()) {
                Thread.sleep(1000L);
            }
            Thread.sleep(1000L);
            ZipUtils.toZip(fileList, response.getOutputStream());
            Thread.sleep(1000L);
            for (File file : fileList) {
                if(file.exists()) {
                    file.delete();
                }

            }
        }



    }

}


